package com.winit.agent.manager;

import com.winit.agent.model.Agent;
import com.winit.agent.model.AgentWorkspace;
import com.winit.agent.repository.AgentRepository;
import com.winit.agent.repository.AgentWorkspaceRepository;
import com.winit.agent.repository.IAgentWorkspaceRepositoryExt;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class AgentWorkspaceManager {

    private static Logger log = LoggerFactory.getLogger(AgentWorkspaceManager.class);

    @Autowired
    private AgentWorkspaceRepository agentWorkspaceRepository;
    @Autowired
    private IAgentWorkspaceRepositoryExt agentWorkspaceRepositoryExt;
    @Autowired
    private AgentRepository agentRepository;

    public List<AgentWorkspace> getAgentWorkspace(String agentId, String wsId, String isDefault){
        return this.agentWorkspaceRepository.findByIdAndAgentIdAndIsDefault(wsId, agentId, isDefault);
    }


    public ExecuteResult updateAgentWorkspace(AgentWorkspace agentWorkspace){
        ExecuteResult executeResult = new ExecuteResult();
        String wsId = agentWorkspace.getWsId();
        if (StringUtils.isEmpty(wsId)){
            log.info("wsId is null, add will be executed.");
            if (StringUtils.isEmpty(agentWorkspace.getAgentId())){
                executeResult.setCode(BaseController.REQUEST_ERROR);
                executeResult.setDesc("agent id cannot be null.");
                return executeResult;
            }
            Agent agent = this.agentRepository.findOne(agentWorkspace.getAgentId());
            if (null == agent){
                executeResult.setCode(BaseController.REQUEST_ERROR);
                executeResult.setDesc("agent cannot be found for agent id:" + agentWorkspace.getAgentId());
                return executeResult;
            }
            wsId = agent.getAgentIp() + "_"  + agent.getAgentPort() + "_" + agentWorkspace.getWsCode();
            agentWorkspace.setWsId(wsId);
            this.agentWorkspaceRepository.save(agentWorkspace);
            executeResult.setData(agentWorkspace.getWsId());
            return executeResult;
        }

        AgentWorkspace entity = this.agentWorkspaceRepository.findOne(wsId);
        if (null == entity){
            executeResult.setCode(BaseController.REQUEST_ERROR);
            executeResult.setDesc("no item found for wsId:" + wsId);
            return executeResult;
        }
        String prdId = agentWorkspace.getPrdId();
        if ("0".equalsIgnoreCase(entity.getIsDefault())&& (!"1".equalsIgnoreCase(agentWorkspace.getIsDefault())) && (!StringUtils.isEmpty(prdId))){
            executeResult.setCode(BaseController.REQUEST_ERROR);
            executeResult.setDesc("default agent workspace cannot related to any prd.");
            return executeResult;
        }
        if (!StringUtils.isEmpty(prdId)){   //prd 与 agent workspace只能是一对一的关系
            List<AgentWorkspace> exists = this.agentWorkspaceRepository.findByPrdId(prdId);
            if (!CollectionUtils.isEmpty(exists)){
                if (exists.size() > 1){
                    executeResult.setCode(BaseController.REQUEST_ERROR);
                    executeResult.setDesc("prd cannot relate to more than one agent workspace");
                    return executeResult;
                }
                if (!wsId.equalsIgnoreCase(exists.get(0).getWsId())){
                    executeResult.setCode(BaseController.REQUEST_ERROR);
                    executeResult.setDesc("prd cannot relate to more than one agent workspace");
                    return executeResult;
                }
            }
        }

        if(!this.agentWorkspaceRepositoryExt.updateByWsIdForPropertyNotNull(agentWorkspace)){
            executeResult.setCode(BaseController.REQUEST_ERROR);
            executeResult.setDesc("no item updated.");
        }
        return executeResult;
    }

    public ExecuteResult operateAgentWorksapce(String wsId, Integer operation){
        ExecuteResult executeResult = new ExecuteResult();
        if (1 == operation.intValue()){
            AgentWorkspace entity = this.agentWorkspaceRepository.findOne(wsId);
            if (null == entity){
                executeResult.setCode(BaseController.REQUEST_ERROR);
                executeResult.setDesc("no agent workspace found for wsId:" + wsId);
                return executeResult;
            }
            if (!StringUtils.isEmpty(entity.getPrdId())){
                executeResult.setCode(BaseController.REQUEST_ERROR);
                executeResult.setDesc("defined item cannot be deleted for related prd exists.");
                return executeResult;
            }
            this.agentWorkspaceRepository.delete(wsId);
        }else {
            executeResult.setCode(BaseController.REQUEST_ERROR);
            executeResult.setDesc("unsupported operation:" + operation);
        }
        return executeResult;
    }
}
